package com.chezai.client.dao.manager;

import android.support.annotation.NonNull;

import com.chezai.client.bean.CoachLoginRecord;
import com.chezai.client.dao.CoachLoginRecordDao;
import com.chezai.client.dao.DaoManager;
import com.chezai.client.global.Const;
import com.chezai.client.utils.DateUtil;

import java.util.List;

/**
 * 教练登录记录表管理类
 * Created by likunyang on 2017/9/22.
 */

public class CoachLoginRecordDaoManager {
    private static CoachLoginRecordDaoManager newInstance;
    private String TAG = this.getClass().getSimpleName();
    private CoachLoginRecordDao dao;

    private CoachLoginRecordDaoManager() {
        dao = DaoManager.getInstance().getDaoSession().getCoachLoginRecordDao();
    }

    public static CoachLoginRecordDaoManager getInstance() {
        if (newInstance == null) {
            newInstance = new CoachLoginRecordDaoManager();
        }
        return newInstance;
    }

    /**
     * 通过教练用户ID，查找当天的登录记录
     * 科一科四 按登录的时间查找
     * 科二科三 按登录的日期查找
     *
     * @param coachInfoId
     * @param subject     登录科目
     * @return
     */
    public CoachLoginRecord findCoachLoginRecordByCurrentDay(long coachInfoId, int subject) {
        String currentDay = DateUtil.timestampStr(System.currentTimeMillis(), "yyMMdd");
        List<CoachLoginRecord> list = dao.queryBuilder()
                .where(CoachLoginRecordDao.Properties.Cid.eq(coachInfoId)
                        , CoachLoginRecordDao.Properties.CreateDay.eq(currentDay)
                        , CoachLoginRecordDao.Properties.TeachSubject.eq(subject))
                .list();
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 插入教练登录记录
     *
     * @param coachInfoId
     * @return
     */
    public CoachLoginRecord insertCoachLoginRecord(@NonNull long coachInfoId, int subject) {
        CoachLoginRecord coachLoginRecord;
        if (subject == Const.TrainPart.TWO || subject == Const.TrainPart.THREE) {
            coachLoginRecord = findCoachLoginRecordByCurrentDay(coachInfoId, subject);
            if (coachLoginRecord != null) {
                return coachLoginRecord;
            }
        }
        coachLoginRecord = new CoachLoginRecord();
        coachLoginRecord.setCid(coachInfoId);
        coachLoginRecord.setCreateDay(DateUtil.timestampStr("yyMMdd"));
        coachLoginRecord.setSignInTime(System.currentTimeMillis());
        coachLoginRecord.setTeachSubject(subject);
        long coachLoginRecordId = dao.insert(coachLoginRecord);
        coachLoginRecord.set_ID(coachLoginRecordId);
        return coachLoginRecord;
    }

    public CoachLoginRecord findCoachLoginRecord(Long coachLoginRecordId) {
        CoachLoginRecord record = dao.load(coachLoginRecordId);
        return record;
    }

    /**
     * 查找科一或科四 未签退的教练
     *
     * @return
     */
    public CoachLoginRecord findOneOrFourUnSignOutCoach() {
        CoachLoginRecord coachLoginRecord = dao.queryBuilder()
                .where(CoachLoginRecordDao.Properties.SignOutTIme.isNull()
                        , CoachLoginRecordDao.Properties.TeachSubject.in(Const.TrainPart.ONE, Const.TrainPart.FOUR))
                .orderDesc(CoachLoginRecordDao.Properties.SignInTime)
                .limit(1)
                .unique();

        return coachLoginRecord;
    }

    public void update(CoachLoginRecord coachLoginRecord) {
        if (coachLoginRecord != null) {
            dao.update(coachLoginRecord);
        }
    }

    /**
     * 更新教练登出时间
     *
     * @param coachLoginRecordId
     */
    public void updateCoachSignoutTime(long coachLoginRecordId) {
        CoachLoginRecord coachLoginRecord = findCoachLoginRecord(coachLoginRecordId);
        coachLoginRecord.setSignOutTIme(System.currentTimeMillis());
        update(coachLoginRecord);
    }
}
